// 链栈头文件

#include <stdio.h>
#include <malloc.h>

#define OK 1
#define ERROR 0

typedef int Status;
typedef int SElemType; //栈中元素的类型

typedef struct SNode {
    SElemType data;
    struct SNode *next;  //后继指针
} SNode, *LinkStackPtr;  //链栈中节点类型

typedef struct LinkStack {
    LinkStackPtr top;  //栈顶指针
    int count;
} LinkStack, *LStack; //链栈类型

void InitStack(LStack &S){
    S=(LStack) malloc(sizeof (LinkStack));
    S->top==NULL;
    S->count=0;
}  //初始化空栈，成功返回OK，失败返回ERROR

Status GetTop(LStack S,SElemType &e){
    if(S->top==NULL)
        return ERROR;
    else{
        e=S->top->data;
        return OK;
    }
} //获取栈顶元素的值，返回在e中，取值成功返回OK，失败返回ERROR

Status Push(LStack &S,SElemType e){
    SNode *p=(SNode*) malloc(sizeof (SNode));
    if(p==NULL)
        return ERROR;
    else{
        p->data=e;
        p->next=S->top;
        S->top=p;
        S->count++;

    }
    return OK;
} //将元素e入栈S

Status Pop(LStack &S,SElemType &e){
    if(S->top==NULL)
        return ERROR;
    else{
        e=S->top->data;
        SNode *p=S->top;
        S->top=S->top->next;
        free(p);
        S->count--;
        return OK;
    }
} //将栈顶元素an出栈，并在e中返回

int StackLength(LStack S){
    return S->count;
} //求栈的长度